package com.cfh.web.xml;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.cfh.service.XmlService;
import com.opensymphony.xwork2.ActionSupport;

public class TuijianXml extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(TuijianXml.class);
	private XmlService xmlService = null;
	private String xml;
	private String answer;
	private String step1;
	private String step2;
	private String step3;
	private String step4;
	
	public String getStep1() {
		return step1;
	}

	public void setStep1(String step1) {
		this.step1 = step1;
	}

	public String getStep2() {
		return step2;
	}

	public void setStep2(String step2) {
		this.step2 = step2;
	}

	public String getStep3() {
		return step3;
	}

	public void setStep3(String step3) {
		this.step3 = step3;
	}

	public String getStep4() {
		return step4;
	}

	public void setStep4(String step4) {
		this.step4 = step4;
	}

	

	public String execute() {
		try {
			logger.debug("tuijian.action,step1=" + step1 + ",step2=" + step2 + ",step3=" + step3 + ",step4=" + step4);
			StringBuffer sb = new StringBuffer();
			for (int i = 1; i <= 4; i++) {
				String stepX = null;
				if (i == 1) {
					stepX = step1;
				}
				if (i == 2) {
					stepX = step2;
				}
				if (i == 3) {
					stepX = step3;
				}
				if (i == 4) {
					stepX = step4;
				}

				if (stepX != null) {
					String[] tmpArray = stepX.split(",");
					for (int j = 0; j < tmpArray.length; j++) {
						String tmpStr = tmpArray[j];
						if (tmpStr != null && tmpStr.trim().length() > 0) {
							sb.append("Q" + i + "_" + addOne(tmpStr) + "^");
						}

					}
				}

			}
			
			answer = sb.toString();
			logger.debug("tuijian.action,answer=" + answer);

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("error in action", e);
			throw new RuntimeException(e);
		}

		try {
			String[] answers = answer.split("\\^");
			HttpServletRequest req = ServletActionContext.getRequest();
			String add = req.getRemoteAddr();
			xml = getXmlService().tuijian(answers, add);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("error in action", e);
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	private String addOne(String tmpStr) {
		int newInt = 1;
		try {
			int oldInt = Integer.parseInt(tmpStr);
			newInt = oldInt + 1;
		} catch (Exception printAndIgnore) {
			printAndIgnore.printStackTrace();
			logger.error("error in action", printAndIgnore);
			newInt = 1;
		}
		
		return String.valueOf(newInt);
	}

	public void setXml(String xml) {
		this.xml = xml;
	}

	public String getXml() {
		return xml;
	}



	public void setXmlService(XmlService xmlService) {
		this.xmlService = xmlService;
	}

	public XmlService getXmlService() {
		return xmlService;
	}

}
